package testbatch.monitor.client;

import java.awt.Color;
import java.util.Date;

import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.swing.BorderFactory;
import javax.swing.JProgressBar;
import javax.swing.border.Border;

import org.apache.commons.lang.time.DateFormatUtils;

import batch.monitor.server.BatchMngMBean;

public class JMXClientListener implements NotificationListener {
	private JMXConnector jmxc = null;
	private String targetObejctName = "";
	
	private JProgressBar progressBar = null;
	private Border border;
	
	public JMXClientListener(JMXConnector jmxConnector, String targetObjectName, JProgressBar progressBar) {
		this.jmxc = jmxConnector;
		this.targetObejctName = targetObjectName;
		this.progressBar = progressBar;
	}
	
	public void handleNotification(Notification notification, Object handback) {
		try {
			
			MBeanServerConnection msbc = jmxc.getMBeanServerConnection();
			ObjectName name = new ObjectName(targetObejctName);
			
			System.out.println("\n\nReceived notification:");
			System.out.println("ClassName: " + notification.getClass().getName());
			System.out.println("Source: " + notification.getSource());
			System.out.println("Type: " + notification.getType());
			System.out.println("Message: " + notification.getMessage());
			System.out.println("=========================================================");
			this.catchMbean(msbc, name);
			System.out.println("=========================================================");
					
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void catchMbean(MBeanServerConnection msbc, ObjectName name) {
		BatchMngMBean batchMngMBean = JMX.newMBeanProxy(msbc, name, BatchMngMBean.class);
		String date = DateFormatUtils.format(new Date(), "hh:mm:ss");
		System.out.println("batchMngMBean start time : " + date);
		System.out.println();
		System.out.println("batchMngMBean.getBatchCfgName() : " + batchMngMBean.getBatchCfgName());
		System.out.println("batchMngMBean.getBatchDescription() : " + batchMngMBean.getBatchDescription());
		System.out.println("batchMngMBean.getBatchFullCount() : " + batchMngMBean.getBatchFullCount());
		System.out.println("batchMngMBean.getBatchCurrentLoopCount() : " + batchMngMBean.getBatchCurrentLoopCount());
		System.out.println("batchMngMBean.getMaxMemory() :  " + batchMngMBean.getMaxMemory());
		System.out.println("batchMngMBean.getTotalMemory() : " + batchMngMBean.getTotalMemory());
		System.out.println("batchMngMBean.getFreeMemory() :  " + batchMngMBean.getFreeMemory());
		System.out.println();
		System.out.println("batchMngMBean.getCompany() : " + batchMngMBean.getCompany());
		System.out.println("batchMngMBean.getItem() : " + batchMngMBean.getItem());
		System.out.println("batchMngMBean.getPerson() : " + batchMngMBean.getPerson());
		System.out.println("batchMngMBean.getQuantity() : " + batchMngMBean.getQuantity());
		
		int progress = (batchMngMBean.getQuantity().intValueExact() * 100) / batchMngMBean.getBatchFullCount();
		border = BorderFactory.createTitledBorder(batchMngMBean.getBatchCfgName());
		progressBar.setBorder(border);
		progressBar.setValue(progress);
		if(progress == 100){
			progressBar.setForeground(Color.GRAY);
		}
	}

}